package dreamCookGame;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;

public class Character {
	
	String _name;
	String _imgName;
	String [] _walkingImgName;
	BufferedImage _img;
	BufferedImage [] _walkingImgs;
	boolean _walkingRight;
	boolean _walkingLeft;
	int _limitLeft;
	int _limitRight;
	boolean _isWalking;
	int _x;
	int _y;
	int _step;
	float _scale;
	
	
	//Behavior
	CharacterBehavior[] _behaviors;
	public boolean _isMainCharacter;
	public CharacterBehavior _currentBehavior;
	
	//Interactivity
	ItemMenu _itemMenu;
	Boolean _menuOn;
	public Button _itemButton;
	String[] _menuOptions;
	Dialogue _dialogue;
	
	public Character(String name, CharacterBehavior[] behaviors, Dialogue dialogue, boolean isMainCharacter, int x, int y) {
		_name = name;
		_x = x;
		_y = y;
		_step = 0;
		_behaviors = behaviors;
		_isMainCharacter = isMainCharacter;
		_menuOptions = null;
		_menuOn = false;
		_dialogue = dialogue;
		_scale = 1;
	}
	public Character(String name, CharacterBehavior[] behaviors, Dialogue dialogue, boolean isMainCharacter, int x, int y, int limitLeft, int limitRight) {
		_name = name;
		_x = x;
		_y = y;
		_step = 0;
		_behaviors = behaviors;
		_isMainCharacter = isMainCharacter;
		_menuOptions = null;
		_menuOn = false;
		_dialogue = dialogue;
		
		_limitLeft = limitLeft;
		_limitRight = limitRight;
		
		_scale = 1;
	}
	
	public void loadResources(References reference) {
		for(int i = 0; i < _behaviors.length; i++) 
			_behaviors[i].loadResources(reference);
		_currentBehavior = _behaviors[0];
		_img = _currentBehavior._currentFrame;

		if(!_isMainCharacter)
		{
			_menuOptions = new String[4];
			_menuOptions[0] = "talkTo";
			_menuOptions[1] = "nullOption";
			_menuOptions[2] = "nullOption";
			_menuOptions[3] = "nullOption";
			_itemMenu = new ItemMenu(_x+_img.getWidth()/2, _y+_img.getHeight()/2, _menuOptions);
			_itemMenu.loadResources(reference);
		}
		

		if(_dialogue != null)
			_dialogue.loadResources(reference);
	}
	
	public void setBehavior(String behaviorName) {		
		for(int i = 0; i < _behaviors.length; i++) {
			if(_behaviors[i]._behaviorName.equals(behaviorName))
				_currentBehavior = _behaviors[i];
		}
	}
	
	public void setDialogue(Dialogue dialogue) {
		_dialogue = dialogue;
	}
	
	public void talk() {
		if(_dialogue != null) {
			_dialogue._currentAudioClip.play();
			_dialogue.setToNextAudioClip();
		}
	}
	
	public void loadWalkingImages(References reference){
		for(int i = 0; i < 5; i++){
			_walkingImgs[i] = reference.getImage(_walkingImgName[i]);
		}
	}
	
	public void walkLeft() {
		_walkingLeft = true;
		_isWalking = true;
	}
	
	public void walkRight() {
		_walkingRight = true;
		_isWalking = true;
	}
	public void stopLeft() {
		_walkingLeft = false;
		_isWalking = false;
	}
	
	public void stopRight() {
		_walkingRight = false;
		_isWalking = false;
	}
	
	private void updateCurrentBehaviorFrame() {
		_currentBehavior.setToNextFrame();
	}
	
	private void updateActiveWalkingFrame() {
		if(_walkingLeft) {
			if(_x > _limitLeft){
				_x -= 15;
				_currentBehavior.setToNextFrame();
			}
			else {
				_x = _limitLeft;
				_walkingLeft = false;
			}
		}
		else if(_walkingRight) {
			if(_x < _limitRight) {
				_x += 15;
				_currentBehavior.setToNextFrame();
			}
			else {
				_x = _limitRight;
				_walkingRight = false;
			}
		}
	}
	
	public void update() {	
		if(!_currentBehavior._behaviorName.equals("walk"))
			updateCurrentBehaviorFrame();
		
		_img = _currentBehavior._currentFrame;
	}
	
	public void updateWalk() {
		if(_isMainCharacter && _currentBehavior._behaviorName.equals("walk"))
			updateActiveWalkingFrame();
		
			_img = _currentBehavior._currentFrame;
	}
	
	public boolean isWithinCharacter(int x, int y) {
		int leftBound = _x;
		int rightBound = _x + _img.getWidth();
		int topBound = _y;
		int bottomBound = _y + _img.getHeight();
		
		if(x >= leftBound && x <= rightBound && y >= topBound && y <= bottomBound)
			return true;
		
		return false;
	}
	
	public void setScale(float scale){
		_scale = scale;
	}
}
